package v1

import (
	"GOPAAS/internal/app/services"
	"GOPAAS/internal/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"os"
	"strconv"
	"strings"
)

// GetCountry 获取国家
func GetCountry(c *gin.Context) {
	lang, _ := c.Get("lang")
	result, err := services.GetCountry(lang.(string))
	if err != nil {
		utils.Error(c, -1, "获取失败", nil)
		return
	}
	utils.Success(c, "success", result)
	return
}

// GetSate 获取省
func GetSate(c *gin.Context) {
	country_id_str := c.Param("country_id")
	country_id, err := strconv.Atoi(country_id_str)
	if err != nil {
		utils.Error(c, -1, "参数错误", nil)
		return
	}
	result, err := services.GetProvince(int64(country_id))
	if err != nil {
		utils.Error(c, -1, "获取失败", nil)
		return
	}
	utils.Success(c, "success", result)
	return
}

// GetCity 获取市
func GetCity(c *gin.Context) {
	state_id_str := c.Param("state_id")
	state_id, err := strconv.Atoi(state_id_str)
	if err != nil {
		utils.Error(c, -1, "参数错误", nil)
		return
	}
	lang, _ := c.Get("lang")
	result, err := services.GetCity(int64(state_id), lang.(string))
	if err != nil {
		utils.Error(c, -1, "获取失败", nil)
		return
	}
	utils.Success(c, "success", result)
	return
}

// GetArea 获取区
func GetArea(c *gin.Context) {
	city_id_str := c.Param("city_id")
	city_id, err := strconv.Atoi(city_id_str)
	if err != nil {
		utils.Error(c, -1, "参数错误", nil)
		return
	}
	result, err := services.GetArea(int64(city_id))
	if err != nil {
		utils.Error(c, -1, "参数错误", nil)
		return
	}
	utils.Success(c, "success", result)
	return
}

// GetDict 字典
func GetDict(c *gin.Context) {
	model := c.Param("model")
	lang, _ := c.Get("lang")
	result := services.GetSelectOption(lang.(string), model)
	utils.Success(c, "success", result)
	return
}

// GetAttachment 获取附件
func GetAttachment(c *gin.Context) {
	model_name := c.Param("model")
	res_id_str := c.Param("res_id")
	var is_login bool
	access_token := c.GetHeader("access_token")
	user_id_str := c.GetHeader("user_id")
	user_id, _ := strconv.Atoi(user_id_str)
	res_id, _ := strconv.Atoi(res_id_str)
	is_login = services.IsLogin(int64(user_id), access_token)

	attachments := services.GetDownloadPath(c, model_name, int64(res_id), is_login)
	utils.Success(c, "success", attachments)
	return
}

// RemoveAttachment 删除附件
func RemoveAttachment(c *gin.Context) {
	id := c.PostForm("id")
	model_name := c.Param("model")
	res_id_str := c.Param("res_id")
	res_id, _ := strconv.Atoi(res_id_str)
	attach_service := services.GetAttachService(0)
	attach, err := attach_service.GetAttachBySum(id)
	if err != nil || attach.Id == 0 {
		utils.Error(c, -1, "附件不存在", nil)
		return
	}
	if attach.ResModel != model_name || attach.ResId != int64(res_id) {
		utils.Error(c, -1, "您不能删除该附件", nil)
		return
	}
	attach_service = services.GetAttachService(attach.Id)
	err = attach_service.DeleteAttach(int64(res_id), model_name, "")
	if err != nil {
		utils.Error(c, -1, fmt.Sprintf("%s", err), nil)
		return
	}
	utils.Success(c, "删除成功", nil)
	return
}

type NewsTab struct {
	NewspropertyRectf1    string `json:"newsproperty_rectf1"`    //推荐
	NewspropertyMartf1    string `json:"newsproperty_martf1"`    //滚动
	NewspropertyHottf1    string `json:"newsproperty_hottf1"`    //热点
	NewspropertyFiltf1    string `json:"newsproperty_filtf1"`    //幻灯
	NewspropertyTttf1     string `json:"newsproperty_tttf1"`     //头条
	NewspropertyCarefully string `json:"newsproperty_carefully"` //精选
	IsPlacement           string `json:"is_placement"`           //置顶
	NewspropertyAnntf1    string `json:"newsproperty_anntf1"`    //公告
	NewspropertyJctf1     string `json:"newsproperty_jctf1"`     //精彩
}

// GetNewsItem 新闻tab
func GetNewsItem(c *gin.Context) {
	utils.Success(c, "success", NewsTab{
		NewspropertyRectf1:    "推荐",
		NewspropertyMartf1:    "滚动",
		NewspropertyHottf1:    "热点",
		NewspropertyFiltf1:    "幻灯",
		NewspropertyTttf1:     "头条",
		NewspropertyCarefully: "精选",
		IsPlacement:           "置顶",
		NewspropertyAnntf1:    "公告",
		NewspropertyJctf1:     "精彩",
	})
	return
}

// GetPartnerTitle 获取职称
func GetPartnerTitle(c *gin.Context) {
	title, err := services.GetTitle()
	if err != nil {
		utils.Error(c, -1, fmt.Sprintf("获取失败：%s", err), nil)
		return
	}
	utils.Success(c, "success", title)
	return
}

// GetProjectTags 获取项目标签
func GetProjectTags(c *gin.Context) {
	title, err := services.GetProjectTags()
	if err != nil {
		utils.Error(c, -1, fmt.Sprintf("获取失败：%s", err), nil)
		return
	}
	utils.Success(c, "success", title)
	return
}

// GetWorkingTime 获取工时选项
func GetWorkingTime(c *gin.Context) {
	company_id, _ := c.Get("company_id")
	work, err := services.GetWorkingTime(company_id.(int64))
	if err != nil {
		utils.Error(c, -1, fmt.Sprintf("获取失败：%s", err), nil)
		return
	}
	utils.Success(c, "success", work)
	return
}

// GetUploaderUrl 上传文件URL
func GetUploaderUrl(c *gin.Context) {
	res_id_str := c.DefaultQuery("res_id", "0")
	folder_id_str := c.DefaultQuery("folder_id", "0")
	field := c.DefaultQuery("field", "")
	model_name := c.Query("model_name")
	user_id, _ := c.Get("user_id")
	company_id, _ := c.Get("company_id")
	res_id_int, err := strconv.Atoi(res_id_str)
	folder_id, err := strconv.Atoi(folder_id_str)
	if err != nil || res_id_int < 0 || folder_id < 0 || model_name == "" {
		utils.Error(c, -1, "参数错误", nil)
		return
	}
	url := services.GetUploaderPath(model_name, int64(res_id_int), field, int64(folder_id), user_id.(int64), company_id.(int64))
	utils.Success(c, "获取成功", url)
	return
}

// GetTypeList 咨询类型
func GetTypeList(c *gin.Context) {
	lang, _ := c.Get("lang")
	type_list, _ := services.GetHelpdeskTypeList(lang.(string))
	utils.Success(c, "获取成功", type_list)
	return
}

// GetLangList 获取语言下拉
func GetLangList(c *gin.Context) {
	type_list, _ := services.GetLang()
	utils.Success(c, "获取成功", type_list)
	return
}

type FileContent struct {
	Content string `json:"content"`
}

// GetFileHtmlContent 获取服务条款和隐私政策
func GetFileHtmlContent(c *gin.Context) {
	type_name := c.Param("type")
	var file_path string
	if type_name == "private" {
		file_path = "./assets/file/隐私政策.html"
	} else if type_name == "service" {
		file_path = "./assets/file/服务条款.html"
	} else {
		utils.HttpError(c, 404, "路由不存在", nil)
		return
	}
	content, err := os.ReadFile(file_path)
	if err != nil {
		utils.Error(c, -1, fmt.Sprintf("获取失败：%s", err), nil)
		return
	}
	utils.Success(c, "获取成功", FileContent{
		Content: string(content),
	})
	return
}

// GetModelFields 获取模型字段
func GetModelFields(c *gin.Context) {
	model := c.Param("model")
	fields := c.Param("fields")
	fields_arr := strings.Split(fields, ",")
	ids := c.Query("ids")
	var id_int64_arr []int64
	if ids != "" {
		id_arr := strings.Split(ids, ",")
		id_int64_arr = utils.StringToInt64Slice(id_arr)
	}
	result := services.GetModelFields(model, fields_arr, id_int64_arr)
	utils.Success(c, "获取成功", result)
	return
}

// GetHrJob 获取岗位
func GetHrJob(c *gin.Context) {
	company_id, _ := c.Get("company_id")
	lang, _ := c.Get("lang")
	work, err := services.GetHrJob(company_id.(int64), lang.(string))
	if err != nil {
		utils.Error(c, -1, fmt.Sprintf("获取失败：%s", err), nil)
		return
	}
	utils.Success(c, "success", work)
	return
}
